package com.huawei.petSystemdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.hms.hwid.I;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.bean.Adress;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.change_shopping_adress_Activity;
import com.huawei.petSystemdemo.ui.mOrder.OrderSureActivity;
import com.huawei.petSystemdemo.ui.pet.Detail_page_Activity;
import com.huawei.petSystemdemo.utils.ImgRepository;
import com.huawei.petSystemdemo.utils.OnBindBottomListener;

import java.util.List;
import java.util.function.Consumer;

public class ShopAdressAdapter  extends RecyclerView.Adapter<ShopAdressAdapter.ShopAdressViewHolder>{
//
    private OnBindBottomListener listener;
    List<Adress> shopAdressList;
    List<Bitmap> bitmapList;
    Context context;
    int order;

    public ShopAdressAdapter(List<Adress> shopAdressList, Context context, int order) {
        this.shopAdressList = shopAdressList;
        this.context = context;
        this.order = order;
    }


    public class ShopAdressViewHolder extends RecyclerView.ViewHolder {
        ImageView tagimg;
        ImageView image;
        TextView person_ID;
        TextView tel;
        TextView shopping_adress_detaillocation;


        public ShopAdressViewHolder(@NonNull View itemView) {
            super(itemView);
            tagimg = itemView.findViewById(R.id.shoppingadress_tag);
            person_ID = itemView.findViewById(R.id.person_ID);
            tel = itemView.findViewById(R.id.tel);//shopping_adress_detaillocation
            shopping_adress_detaillocation = itemView.findViewById(R.id.shopping_adress_detaillocation);
            image = itemView.findViewById(R.id.im);
        }
    }

    @NonNull
    @Override
    public ShopAdressViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
               .inflate(R.layout.comment_shopadress, parent, false);
       return new ShopAdressAdapter.ShopAdressViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ShopAdressViewHolder holder, int position) {
       Adress adress = shopAdressList.get(position);

        ImgRepository.getImage("/shopadress/getImg?img=" + adress.getimg(), new Consumer<Bitmap>() {
            @Override
            public void accept(Bitmap bitmap) {

                holder.tagimg.setImageBitmap(bitmap);
            }
        });
        //holder.img.setImageResource(R.drawable.cat);
        holder.person_ID.setText(adress.getUserName()+"");
        holder.tel.setText(adress.getTel() +"");
        holder.shopping_adress_detaillocation.setText(adress.getAdressContent());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(order == 1) {
                    Intent intent = new Intent(context, OrderSureActivity.class);
                    intent.putExtra("addId", adress.getId());
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, change_shopping_adress_Activity.class);
                    intent.putExtra("id", adress.getId());
                    System.out.println("id:" + adress.getId());
                    context.startActivity(intent);
                }
            }
        });



        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, change_shopping_adress_Activity.class);
                intent.putExtra("id", adress.getId());
                System.out.println("id:" + adress.getId());
                context.startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount() {
        return shopAdressList.size();
    }

}
